﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Easily
{
    public partial class frmTimeArrive : Form
    {
        #region 对外的属性和私有成员
        //private int milliSecond;
        ///// <summary>
        ///// 需要延时的毫秒数
        ///// </summary>
        //public int milliSecond
        //{
        //    get { return this.milliSecond; }
        //    set
        //    {
        //        if (value > 0 && this.MyIcon != null)
        //        {
        //            this.MyIcon.code.Comment = "延时" + value.ToString() + "毫秒";
        //            this.MyIcon.code.InsertCode = "Delay_ms(" +
        //                value.ToString() + ");//" + this.MyIcon.code.Comment;
        //            this.milliSecond = value;
        //        }
        //        else if (value == 0 && this.MyIcon != null)
        //        {
        //            this.MyIcon.code.Comment = "";
        //            this.MyIcon.code.InsertCode = "";
        //            this.milliSecond = value;
        //        }
        //    }
        //}
        /// <summary>
        /// 存储传入的图标控件
        /// </summary>
        private GeneralIcon MyIcon = null;
        #endregion

        #region 构造函数和对外的方法
        /// <summary>
        /// 类的构造函数
        /// 功能概要：保存控件对象；设置默认延时1000毫秒
        /// </summary>
        /// <param name="icon"></param>
        public frmTimeArrive(GeneralIcon icon)
        {
            InitializeComponent();
            this.MyIcon = icon;
            if (this.MyIcon.code.Save == "")
            {
                this.MyIcon.code.Save = "1000";//默认延时1000毫秒
            }
            this.numericUpDowntimedelay.Value = Convert.ToDecimal(this.MyIcon.code.Save);
            createCode();
            //buttonEnsure_Click(new object(), new EventArgs());//生成默认代码
        }
        #endregion

        #region 事件
        /// <summary>
        /// 点击“确定”按钮
        /// 功能概要：设置延时
        /// </summary>
        private void buttonEnsure_Click(object sender, EventArgs e)
        {
            createCode();
            //此句不能少,只有DialogResult=OK才能保存Code 的设置信息。在GeneralIcon.cs1006行要用此结果
            this.DialogResult = DialogResult.OK;//表示当窗体用作对话框时该窗体的结果，
            this.Close();
        }

        /// <summary>
        /// 点击“取消”按钮
        /// 功能概要：退出对话框，并设置对话框结果为取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否要退出设置？", "设置", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
        }

        /// <summary>
        /// 点击“注释说明”按钮
        /// 功能概要：为控件添加自定义注释说明
        /// </summary>
        private void btn_comment_Click(object sender, EventArgs e)
        {
            using (frmNodeEditer NE = new frmNodeEditer(this.MyIcon.code.Comment))
            {
                if (NE.ShowDialog() == DialogResult.OK)
                {
                    this.MyIcon.code.Comment = NE.NoteEdit;
                }
            }
        }
        #endregion
        #region 内部函数
        void createCode()
        {
            MyIcon.code.Clear();//清空以前代码
            int milliSecond = (int)numericUpDowntimedelay.Value;
            //添加头文件、初始化代码等
            MyIcon.code.HeadFile = ""; //添加构件的头文件
            MyIcon.code.Initialization = "timer_init(TIMERA," + milliSecond.ToString() + ");//初始化定时器为"
                + milliSecond.ToString() + "毫秒";
            MyIcon.code.Comment = "初始化定时器为" + milliSecond.ToString() + "毫秒";
            MyIcon.code.InterruptEnable = "timer_enable_int(TIMERA);//开启定时器中断";
            MyIcon.code.InterruptServiceStart = "";
            MyIcon.code.InterruptServiceEnd = "";
            MyIcon.code.Save = milliSecond.ToString();
        }
        #endregion
    }
}
